package br.ufrj.dcc.thalles;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Operacao extends ListActivity {
	String conta;
	String senha;
	Bundle dados;
	String[] opcoes;
	char tipo;
	Intent proximo;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dados=getIntent().getExtras();
		conta = dados.getString("conta");
		senha = dados.getString("senha");
		if ((conta==null)||(senha==null)){
			Toast.makeText(this, "dados de login não encontrados", Toast.LENGTH_SHORT).show();
			Intent voltarAoLogin = new Intent(Operacao.this,Entrar.class);
			//startActivity(voltarAoLogin);
			//finish();
		}
		else{
			AccountResolver accResolver=new AccountResolver(conta,senha);
			tipo = accResolver.getType();
			if ((tipo!='n')&&(tipo!='v')){
				Toast.makeText(this, "Erro ao decidir tipo de usuario", Toast.LENGTH_SHORT).show();
				Intent voltarAoLogin = new Intent(Operacao.this,Entrar.class);
				//startActivity(voltarAoLogin);
				//finish();
			}
		}

		opcoes = getOpcoes();
		ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, opcoes);
		setListAdapter(adapter);
	}

	public String[] getOpcoes()
	{	
		String[] testValues;
		// Create some mock data
		if (tipo=='n'){
			testValues = new String[] {
					getString(R.string.verSaldo),
					getString(R.string.saque),
					getString(R.string.deposito),
					getString(R.string.transferencia),
					getString(R.string.extrato)
			};
		}
		else{
			testValues = new String[] {
					getString(R.string.verSaldo),
					getString(R.string.saque),
					getString(R.string.deposito),
					getString(R.string.transferencia),
					getString(R.string.extrato),
					getString(R.string.solicitarGerente)
			};

		}

		return testValues;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position,long id){
		String item = (String) getListAdapter().getItem(position);
		Toast.makeText(this, opcoes[(int)id]+" "+opcoes[(int)position], Toast.LENGTH_SHORT).show();
		organizarOperacao(position);
	}

	private void organizarOperacao(int position) {
		
	switch (position) {
			case 0:
				proximo = new Intent(Operacao.this,VerSaldo.class);
	
				break;
			case 1:
				proximo = new Intent(Operacao.this,Saque.class);
				
				break;
			case 2:
				proximo = new Intent(Operacao.this,Deposito.class);
	
				break;
			case 3:
				proximo = new Intent(Operacao.this,Transferencia.class);
				
				break;
			case 4:
				proximo = new Intent(Operacao.this,Extrato.class);
	
				break;
			case 5:
				proximo = new Intent(Operacao.this,SolicitarGerencia.class);
	
				break;
	
			default:
				break;
		}
	proximo.putExtras(dados);
	startActivity(proximo);


	}

}
